var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    footprintList: [],
    page: 1,
    size: 10,
  },
  //增加下拉刷新数据的功能
  onPullDownRefresh(){
    var that = this;
    that.reloadFootprintList();
  },
  //得到足迹列表
  getFootprintList() {
    let that = this;
    var tmpFootPrint;
    util.request(api.FootprintList).then(function (res) {
      if (res.errno === 0) {
        if (res.data.data != undefined){
          tmpFootPrint = res.data.data;
        } else {
          tmpFootPrint = [];
        }
        that.setData({
          footprintList: that.data.footprintList.concat(tmpFootPrint),
        });
      }
    });
  },
  //删除足迹
  deleteItem (event){
    let that = this;
    let footprint = event.currentTarget.dataset.footprint;
    var touchTime = that.data.touch_end - that.data.touch_start;
    //如果按下时间大于350为长按
    if (touchTime > 350) {
      wx.showModal({
        title: '',
        content: '要删除所选足迹？',
        success: function (res) {
          if (res.confirm) {
            util.request(api.FootprintDelete, { footprintId: footprint.id }).then(function (res) {
              if (res.errno === 0) {
                wx.showToast({
                  title: res.errmsg,
                  icon: 'success',
                  duration: 2000,
                  complete:function(){
                    that.reloadFootprintList();
                  }
                });
              } else{
                util.showErrorToast(res.errmsg);
              }
            });
          }
        }
      });
    } else {
      wx.navigateTo({
        url: '/pages/goods/goods?id=' + footprint.goods_id,
      });
    }
  },
  //页面加载
  onLoad: function (options) {
    var that = this;
    that.reloadFootprintList();
  },
  //按下事件开始  
  touchStart: function (e) {
    let that = this;
    that.setData({
      touch_start: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-start')
  },
  //按下事件结束  
  touchEnd: function (e) {
    let that = this;
    that.setData({
      touch_end: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-end')
  },
  //重新加载
  reloadFootprintList:function(){
    var that = this;
    that.setData({
      footprintList: [],
      page: 1,
      size: 10,
    });
    that.getFootprintList();
  },
})